package com.dc.web;

import com.dc.dao.BlogRepository;
import com.dc.po.Blog;
import com.dc.po.Type;
import com.dc.service.BlogService;
import com.dc.service.TypeService;
import com.dc.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class TypeShowController {

    @Autowired
    private TypeService typeService;
    @Autowired
    private BlogService blogService;



    //根据分类id查询文章
//restful    th:href="@{'/types/'+${activeTypeId}(page=${page.number}-1)}"
    @GetMapping("/types")
    public String types(@PageableDefault(size = 6, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                        @RequestParam Long id,
                        Model model){
        List<Type> types = typeService.listTypeTop(100);
        /*如果id==-1说明是首页跳过来的，给分类Top1的id*/
        if(id==-1){
           id = types.get(0).getId();
        }
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id);

        model.addAttribute("types",types);
        model.addAttribute("page",blogService.listBlog(pageable,blogQuery));
        model.addAttribute("activeTypeId",id);
        model.addAttribute("type",typeService.getType(id));
        return "types";
    }
}
